/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package workforce.limousine.service;

import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;

/**
 *
 * @author TEComp
 */
public class SendMailBaseApp implements Runnable, MailApp {

    private String from = "";
    private String to = "";
    private String subject = "";
    private String text = "";
    private String fileName = "";
    Properties props = new Properties();
    private byte[] bytes = null;
    public SendMailBaseApp() {
    }

    @Override
    public void setFrom(String f) {
        this.from = f;
    }

    @Override
    public void setTo(String to) {
        this.to = to;
    }

    @Override
    public void setSubject(String subject) {
        this.subject = subject;
    }

    @Override
    public void setText(String text) {
        this.text = text;
    }
    
    public void setFileName(String f) {
        this.fileName = f;
    }

    @Override
    public void run() {
        send();
    }

    public void setContent(byte[] b) {
        bytes = b;
    }
    
    public void send() {


        props.put("mail.smtplhost", "smtp.gmail.com");
        props.put("mail.smtp.port", "465");
        Session session = Session.getInstance(props, null);

        try {
            // create a message
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(from));
            InternetAddress[] address = {new InternetAddress(to)};
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject(subject);

            // create and fill the first message part
            MimeBodyPart mbp1 = new MimeBodyPart();
            mbp1.setText(text);

            // create the second message part
            MimeBodyPart mbp2 = new MimeBodyPart();
            // attach the file to the message
            javax.mail.util.ByteArrayDataSource bds = new ByteArrayDataSource(bytes, "application/pdf");
            mbp2.setDataHandler(new DataHandler(bds));
           
            mbp2.setFileName(fileName);

            // create the Multipart and add its parts to it
            Multipart mp = new MimeMultipart();
            mp.addBodyPart(mbp1);
            mp.addBodyPart(mbp2);

            // add the Multipart to the message
            msg.setContent(mp);

            // set the Date: header
            msg.setSentDate(new Date());

            // send the message
            Transport.send(msg);

        } catch (MessagingException mex) {
            mex.printStackTrace();
            Exception ex = null;
            if ((ex = mex.getNextException()) != null) {
                ex.printStackTrace();
            }

        }
    }
}
